package com.hjb.service.controller.recharge;

import com.hjb.service.domain.product.SiteUserBuy;
import com.hjb.service.domain.product.SiteUserBuyExample;
import com.hjb.service.domain.recharge.SiteUserRecharge;
import com.hjb.service.domain.siteuser.SiteUser;
import com.hjb.service.gateway.baofoo.domain.request.TransReqBF0040002;
import com.hjb.service.product.SiteUserBuyService;
import com.hjb.service.recharge.RechargeService;
import com.hjb.service.utils.DateUtil;
import com.hjb.service.utils.MapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gy on 2017/7/28.
 * 充值
 */
@RestController
@RequestMapping("/recharge")
public class RechargeController {
	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private RechargeService rechargeService;
	@Autowired
	private SiteUserBuyService siteUserBuyService;

	/**
	 * 用户充值获取验证码
	 * @param request
	 * @return
	 */
	@RequestMapping("/recharge")
	@ResponseBody
	public Map<String, Object> recharge(HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			String userId = request.getParameter("userId");//2001
			String amount = request.getParameter("amount");//new BigDecimal("10")
			String orderId = request.getParameter("orderId");//String.valueOf(System.currentTimeMillis()+siteUserBuy.getUserId())

			SiteUser siteUser = (SiteUser) request.getSession().getAttribute("siteUser");
			if (siteUser == null){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE_416);
				resMap.put("result_info", "获取验证码失败，请重新登录");
				return resMap;
			}
			if (!siteUser.getUserId().equals(userId)){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE_416);
				resMap.put("result_info", "获取验证码失败，请重新登录");
				return resMap;
			}
			SiteUserBuyExample example = new SiteUserBuyExample();
			example.createCriteria().andUserIdEqualTo(Integer.valueOf(userId))
					.andAmountEqualTo(new BigDecimal(amount)).andOrderNoEqualTo(orderId).andStatusEqualTo(0);
			List<SiteUserBuy> siteUserBuyList = siteUserBuyService.selectByExample(example);
			if (siteUserBuyList == null || siteUserBuyList.size() == 0){
				resMap.put("result_code", MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "不存在购买记录，交易单号："+ orderId +"交易金额："+ amount);
				return resMap;
			}
			SiteUserBuy siteUserBuy = siteUserBuyList.get(0);
			resMap = rechargeService.recharge(siteUserBuy);
		}catch (Exception e){
			log.info("充值失败");
		}
		return resMap;
	}

	/**
	 * 用户确认充值
	 * @param request
	 * @return
	 */
	@RequestMapping("/rechargeConfirm")
	@ResponseBody
	public Map<String, Object> rechargeConfirm(HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			String smsCode = request.getParameter("smsCode");
			String userId = request.getParameter("userId");//2001
			String amount = request.getParameter("amount");//new BigDecimal("10")
			String orderId = request.getParameter("orderId");//String.valueOf(System.currentTimeMillis()+siteUserBuy.getUserId())

			SiteUser siteUser = (SiteUser) request.getSession().getAttribute("siteUser");
			if (siteUser == null){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE_416);
				resMap.put("result_info", "获取验证码失败，请重新登录");
				return resMap;
			}
			if (!siteUser.getUserId().equals(userId)){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE_416);
				resMap.put("result_info", "获取验证码失败，请重新登录");
				return resMap;
			}
			SiteUserBuyExample example = new SiteUserBuyExample();
			example.createCriteria().andUserIdEqualTo(Integer.valueOf(userId))
					.andAmountEqualTo(new BigDecimal(amount)).andOrderNoEqualTo(orderId).andStatusEqualTo(0);
			List<SiteUserBuy> siteUserBuyList = siteUserBuyService.selectByExample(example);
			if (siteUserBuyList == null || siteUserBuyList.size() == 0){
				resMap.put("result_code", MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "不存在购买记录，交易单号："+ orderId +"交易金额："+ amount);
				return resMap;
			}
			SiteUserBuy siteUserBuy = siteUserBuyList.get(0);
			resMap = rechargeService.rechargeConfirm(siteUserBuy,smsCode);
		}catch (Exception e){
			log.info("充值失败");
		}
		return resMap;
	}

	/**
	 * 充值查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/rechargeQuery")
	@ResponseBody
	public Map<String, Object> rechargeQuery(HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SiteUserRecharge siteUserRecharge = new SiteUserRecharge();
			siteUserRecharge.setOrderIdConf(String.valueOf(System.currentTimeMillis()+2001));
			siteUserRecharge.setReplyTime(new Date());
			rechargeService.rechargeQuery(siteUserRecharge);
		}catch (Exception e){
			log.info("充值失败");
		}
		return resMap;
	}
}
